package com.sun.juc.sync;

public class ThreadDemo01 {
    public static void main(String[] args) {
        Shar shar = new Shar();
        new Thread(()->{while (true)shar.incrNumber();},"AA").start();
        new Thread(()->{while (true)shar.decrNumber();},"BB").start();
    }
}
class Shar{
    private int number = 0;

    //加1
    public synchronized void incrNumber(){
        //判断  干活  通知
        if (number != 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "::" + number);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.notifyAll();
    }
    //减1
    public synchronized void decrNumber(){
        //判断  干活  通知
        if (number == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "::" + number);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.notifyAll();
    }
}
